package data_structure.test;

class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(4);
        System.out.println("====添加操作====");
        arrayQueue.addQueue(1);
        arrayQueue.addQueue(2);
        arrayQueue.addQueue(3);
        arrayQueue.addQueue(4);
        System.out.println("====遍历队列====");
        arrayQueue.selectQueue();
        System.out.println("====出队操作====");
        System.out.println(arrayQueue.outQueue());
        System.out.println(arrayQueue.outQueue());
        System.out.println(arrayQueue.outQueue());
        System.out.println(arrayQueue.outQueue());
    }
}

class ArrayQueue{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    //初始化队列
    public ArrayQueue(Integer maxSize) {
        this.maxSize = maxSize;
        front = -1;
        rear = -1;
        arr = new int[this.maxSize];
    }

    //显示队列中数据
    public void selectQueue(){
        if (isEmpty()){
            System.out.println("队列为空，无法执行遍历队列操作");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i != arr.length-1 ? arr[i]+" ":arr[i]+"\n");
        }
    }

    //出队
    public int outQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空，无法执行出队操作...");
        }
        return arr[++front];
    }

    //入队
    public void addQueue(int n){
        if (isFull()){
            System.out.println("队列已满，无法添加...");
            return;
        }
        arr[++rear] = n;
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }

    //判断队列是否满
    public boolean isFull(){
        return rear == this.maxSize-1;
    }
}
